﻿using System;
using System.Collections.Generic;

using Warehouse.Silverlight.Core.Infrastructure.Components;

namespace Warehouse.Silverlight.Modules.Main.Models
{
    public class ProductSearchParams : SearchParams
    {
        public const string CategoryIdKey = "CategoryId";
        public const string Date1Key = "Date1";
        public const string Date2Key = "Date2";
        public const string ProductCodeKey = "ProductCode";
        public const string ProductNameKey = "ProductName";

        public ProductSearchParams()
        {
        }

        protected override Dictionary<string, object> _default
        {
            get
            {
                var d = new Dictionary<string, object>();
                d.Add(CategoryIdKey, -1);
                d.Add(Date1Key, DateTime.Today);
                d.Add(Date2Key, DateTime.Today);
                d.Add(ProductCodeKey, string.Empty);
                d.Add(ProductNameKey, string.Empty);

                return d;
            }
        }

        protected override bool CompareDictionaryValue(Dictionary<string, object> dict1, Dictionary<string, object> dict2)
        {
            foreach (var item in dict1)
            {
                if (!CompareValue(item.Key, item.Value, dict2[item.Key]))
                    return false;
            }
            return true;
        }

        private bool CompareValue(string key, object obj1, object obj2)
        {
            switch (key)
            {
                case CategoryIdKey:
                    return (obj1 as int?) == (obj2 as int?);
                case Date1Key:
                case Date2Key:
                    return (obj1 as DateTime?) == (obj2 as DateTime?);
                case ProductCodeKey:
                case ProductNameKey:
                    return (obj1 as string) == (obj2 as string);
                default:
                    throw new ArgumentOutOfRangeException(key);
            }
        }

    }
}
